load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//pkg/testutils:buildutil/buildutil.bzl", "disallowed_imports_test")

go_library(
    name = "memo",
    srcs = [
        "check_expr.go",
        "constraint_builder.go",
        "cost.go",
        "expr.go",
        "expr_format.go",
        "expr_name_gen.go",
        "extract.go",
        "filters_expr_mutate_checker.go",
        "group.go",
        "insert_fastpath.go",
        "interner.go",
        "logical_props_builder.go",
        "memo.go",
        "multiplicity_builder.go",
        "statistics_builder.go",
        "typing.go",
        ":gen-expr",  # keep
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/opt/memo",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/geo/geoindex",
        "//pkg/kv/kvserver/concurrency/isolation",
        "//pkg/sql/catalog/colinfo",
        "//pkg/sql/catalog/tabledesc",
        "//pkg/sql/inverted",
        "//pkg/sql/mutations",
        "//pkg/sql/opt",
        "//pkg/sql/opt/cat",
        "//pkg/sql/opt/constraint",
        "//pkg/sql/opt/invertedexpr",  # keep
        "//pkg/sql/opt/props",
        "//pkg/sql/opt/props/physical",
        "//pkg/sql/pgwire/pgcode",
        "//pkg/sql/rowenc/keyside",
        "//pkg/sql/rowenc/valueside",
        "//pkg/sql/sem/cast",
        "//pkg/sql/sem/catid",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/sem/tree/treewindow",
        "//pkg/sql/sem/volatility",
        "//pkg/sql/stats",
        "//pkg/sql/types",
        "//pkg/util/buildutil",
        "//pkg/util/duration",
        "//pkg/util/encoding",
        "//pkg/util/intsets",
        "//pkg/util/iterutil",
        "//pkg/util/json",
        "//pkg/util/log",
        "//pkg/util/timeutil/pgdate",
        "//pkg/util/treeprinter",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_cockroachdb_redact//:redact",
    ],
)

go_test(
    name = "memo_test",
    size = "medium",
    srcs = [
        "cost_test.go",
        "expr_test.go",
        "interner_test.go",
        "logical_props_builder_test.go",
        "memo_test.go",
        "multiplicity_builder_test.go",
        "statistics_builder_test.go",
        "typing_test.go",
    ],
    data = glob(["testdata/**"]) + [
        "//c-deps:libgeos",
        "//pkg/sql/opt/testutils/opttester:testfixtures",
    ],
    embed = [":memo"],
    deps = [
        "//pkg/kv/kvserver/concurrency/isolation",
        "//pkg/settings/cluster",
        "//pkg/sql/inverted",
        "//pkg/sql/opt",
        "//pkg/sql/opt/cat",
        "//pkg/sql/opt/constraint",
        "//pkg/sql/opt/norm",
        "//pkg/sql/opt/optbuilder",
        "//pkg/sql/opt/props",
        "//pkg/sql/opt/props/physical",
        "//pkg/sql/opt/testutils",
        "//pkg/sql/opt/testutils/opttester",
        "//pkg/sql/opt/testutils/testcat",
        "//pkg/sql/opt/xform",
        "//pkg/sql/parser",
        "//pkg/sql/randgen",
        "//pkg/sql/sem/builtins",
        "//pkg/sql/sem/builtins/builtinsregistry",
        "//pkg/sql/sem/catconstants",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/sem/tree/treewindow",
        "//pkg/sql/sessiondata",
        "//pkg/sql/types",
        "//pkg/testutils",
        "//pkg/testutils/datapathutils",
        "//pkg/util/duration",
        "//pkg/util/intsets",
        "//pkg/util/leaktest",
        "//pkg/util/timeofday",
        "//pkg/util/timeutil/pgdate",
        "@com_github_cockroachdb_datadriven//:datadriven",
        "@com_github_cockroachdb_errors//:errors",
    ],
)

# Define a gerator for expression definitions and functions.
genrule(
    name = "gen-expr",
    srcs = ["//pkg/sql/opt:ops"],
    outs = ["expr.og.go"],
    cmd = """
      $(location //pkg/sql/opt/optgen/cmd/optgen) -out $@ exprs $(locations //pkg/sql/opt:ops)
    """,
    tools = ["//pkg/sql/opt/optgen/cmd/optgen"],
    visibility = ["//visibility:public"],
)

disallowed_imports_test(
    "memo",
    ["//pkg/sql/sem/builtins"],
)
